﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ApplicationGenerator.BusinessModule.Interfaces;

namespace ApplicationGenerator.Forms.Utilities
{
    public class TextFieldsConverter
    {
        public static object ConvertToValueField(IModel modelElement, string fieldName, string fieldValue)
        {
            Type fieldType = modelElement.MetaModel.MetaFieldCollection[fieldName].FieldType;
            if (fieldType == typeof(int))
            {
                return string.IsNullOrEmpty(fieldValue) ? default(int) : Convert.ToInt32(fieldValue);
            }

            if (fieldType == typeof(float))
            {
                return string.IsNullOrEmpty(fieldValue) ? default(float) : Convert.ToInt32(fieldValue);
            }

            if (fieldType == typeof(double))
            {
                return string.IsNullOrEmpty(fieldValue) ? default(double) : Convert.ToDouble(fieldValue);
            }

            if (fieldType == typeof(string))
            {
                return fieldValue;
            }

            if (fieldType == typeof(DateTime))
            {
                return string.IsNullOrEmpty(fieldValue) ? default(DateTime) : Convert.ToDateTime(fieldValue);
            }

            return null;
        }

        public static string ConvertToTextField(IModel modelElement, string fieldName)
        {
            Type fieldType = modelElement.MetaModel.MetaFieldCollection[fieldName].FieldType;

            if (fieldType == typeof(int) ||
                fieldType == typeof(float) ||
                fieldType == typeof(double) ||
                fieldType == typeof(string))
            {
                return Convert.ToString(modelElement.GetProperty(fieldName));
            }

            if (modelElement.MetaModel.MetaFieldCollection[fieldName].FieldType == typeof(DateTime))
            {
                return ((DateTime)modelElement.GetProperty(fieldName)).ToString("dd-mm-yyyy");
            }

            return string.Empty;
        }
    }
}
